<template>
  <div class="dashboard-container">
    <div class="app-container">
      <meta name="referrer" content="never" />
      <el-card shadow="never">
        <div class="top">
          <el-form :inline="true" :model="form" class="header demo-form-inline">
            <el-form-item label="关键字" class="left" style="width: 500px">
              <el-input
                v-model="form.keyword"
                placeholder="请根据编号搜索"
                size="small"
              ></el-input>
            </el-form-item>
            <el-form-item class="right">
              <el-button @click="delForm" size="small">清除</el-button>
              <el-button type="primary" @click="search" size="small"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
          <el-alert
            :title="`数据一共${this.total}条`"
            type="info"
            show-icon
            :closable="false"
          >
          </el-alert>
        </div>
        <div class="body">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="id" label="编号"> </el-table-column>
            <el-table-column prop="questionType" label="题型">
              <template #default="{ row }">
                {{ questionTypeForm(row.questionType) }}
              </template>
            </el-table-column>
            <el-table-column prop="questionIDs" label="题目编号">
              <template #default="{ row }">
                <div v-for="(item, index) in row.questionIDs" :key="index">
                  <el-button @click="detail(item)" type="text" size="small">
                    {{ item.number }}
                  </el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="addTime" label="录入时间"> </el-table-column>
            <el-table-column prop="totalSeconds" label="答题时间（s）">
            </el-table-column>
            <el-table-column prop="accuracyRate" label="正确率（%）">
            </el-table-column>
            <el-table-column prop="userName" label="录入人"> </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button
                  @click="delRandom(row)"
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  plain
                >
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="footer">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            :current-page="this.params.page"
            :page-sizes="[20, 30, 50, 100]"
            layout="prev, pager, next,sizes,jumper"
            :total="total"
            :page-size="this.params.pagesize"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <QuestionPreview
      :dialogVisible="dialogVisible"
      :showVideo="showVideo"
      :detailList="detailList"
      :radio="radio"
      @close-dialog="close"
      @question-type="questionTypeForm"
      @open-video="openVideo"
    ></QuestionPreview>
  </div>
</template>

<script>
import { randoms as getRandomsAPI, removeRandoms, detail as detailAPI } from '@/api/hmmm/questions.js'
import QuestionPreview from '../components/questions-preview.vue'

export default {
  components: {
    QuestionPreview
  },
  data () {
    return {
      form: {
        keyword: ''
      },
      tableData: [],
      params: {
        page: 1,
        pagesize: 20
      },
      total: 0,
      dialogVisible: false,
      detailList: {},
      showVideo: false,
      radio: 1
    }
  },
  mounted () {
    this.getRandoms()
  },
  methods: {
    // 获取组题列表
    async getRandoms () {
      const res = await getRandomsAPI(this.params)
      // console.log(res)
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    // 切换分页
    handleCurrentChange (page) {
      this.params.page = page
      this.getRandoms()
    },
    // 每页条数改变
    handleSizeChange (val) {
      this.params.pagesize = val
      this.params.page = 1
      this.getRandoms()
    },
    // 格式化题型
    questionTypeForm (type) {
      const map = {
        1: '单选',
        2: '多选',
        3: '简答'
      }
      return map[type]
    },
    // 点击清除，清空输入框
    delForm () {
      this.form = {
        id: ''
      }
      this.params.page = 1
    },
    // 点击删除——删除当前数据
    delRandom (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(row)
        await removeRandoms(row)
        // 解决最后一页删除数据问题
        if (this.tableData.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        this.getRandoms()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击搜索，获取对应的数据
    async search () {
      const res = await getRandomsAPI(this.form)
      this.params.page = 1
      this.total = res.data.counts
      this.tableData = res.data.items
    },
    // 关闭弹窗
    close () {
      this.dialogVisible = false
      this.showVideo = false
    },
    // 打开视频
    openVideo () {
      this.showVideo = true
    },
    // 点击题目编号--出现弹框，回显数据
    async detail (item) {
      this.dialogVisible = true
      const res = await detailAPI(item)
      this.detailList = res.data
      // console.log(this.detailList)
      if (res.data.questionType === '2') {
        res.data.options.forEach(item => {
          if (item.isRight === 0) {
            item.isRight = false
          } else if (item.isRight === 1) {
            item.isRight = true
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
}
.body {
  margin-top: 14px;
}
.input {
  display: inline-block;
}
.questionIds {
  color: rgb(0, 153, 255);
}
.footer {
  margin-top: 20px;
  display: flex;
  justify-content: end;
}
</style>
